package org.example.service;

import org.example.model.BaseModel;
import org.example.repository.BaseRepository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * AbstractBaseService
 */
public abstract class AbstractBaseService<T extends BaseModel> implements BaseService<T> {
    protected final BaseRepository<T> repository;

    public AbstractBaseService(BaseRepository<T> repository) {
        this.repository = repository;
    }

    @Override
    public List<T> getAll() {
        List<T> list= repository.findAll();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }

        return list;
    }

    @Override
    public T getOne(Long id) {
        return repository.findById(id);
    }

    @Override
    public Long create(T entity) {
        repository.save(entity);
        return entity.getId();
    }

    @Override
    public void update(T entity) {
        repository.update(entity);
    }

    @Override
    public void delete(Long id) {
        repository.deleteById(id);
    }
}
